<?xml version='1.0' ?>
<xsl:stylesheet
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:dia="http://www.lysator.liu.se/~alla/dia/"
	version='1.0'
	>

<!--

This stylesheet transforms the output of 'show full columns from *' and 'show index from *' in mysql+XML
into an un-gzipped xml dia file.
-->
<xsl:output method="xml" version="1.0" encoding="UTF-8"
     	indent="yes"
	/>

<!-- ================ DOCUMENT ROOT ========================================================== -->
<xsl:template match="/">
<dia:diagram>
<xsl:comment>
Generated with sql2dia_table.xsl
Author: Pierre Lindenbaum PhD. (plindenbaum@yahoo.fr). Rewriten by Neptune Ning (frostyplanet@gmail.com) using dia's Table class and adding index information.
</xsl:comment>
  <dia:diagramdata>
    <dia:attribute name="background">
      <dia:color val="#aaaaaa"/>
    </dia:attribute>
    <dia:attribute name="pagebreak">
      <dia:color val="#000099"/>
    </dia:attribute>
    <dia:attribute name="paper">
      <dia:composite type="paper">
        <dia:attribute name="name">
          <dia:string>#A4#</dia:string>
        </dia:attribute>
        <dia:attribute name="tmargin">
          <dia:real val="2.8222000598907471"/>
        </dia:attribute>
        <dia:attribute name="bmargin">
          <dia:real val="2.8222000598907471"/>
        </dia:attribute>
        <dia:attribute name="lmargin">
          <dia:real val="2.8222000598907471"/>
        </dia:attribute>
        <dia:attribute name="rmargin">
          <dia:real val="2.8222000598907471"/>
        </dia:attribute>
        <dia:attribute name="is_portrait">
          <dia:boolean val="true"/>
        </dia:attribute>
        <dia:attribute name="scaling">
          <dia:real val="1"/>
        </dia:attribute>
        <dia:attribute name="fitto">
          <dia:boolean val="false"/>
        </dia:attribute>
      </dia:composite>
    </dia:attribute>
    <dia:attribute name="grid">
      <dia:composite type="grid">
        <dia:attribute name="width_x">
          <dia:real val="1"/>
        </dia:attribute>
        <dia:attribute name="width_y">
          <dia:real val="1"/>
        </dia:attribute>
        <dia:attribute name="visible_x">
          <dia:int val="1"/>
        </dia:attribute>
        <dia:attribute name="visible_y">
          <dia:int val="1"/>
        </dia:attribute>
        <dia:composite type="color"/>
      </dia:composite>
    </dia:attribute>
    <dia:attribute name="color">
      <dia:color val="#d8e5e5"/>
    </dia:attribute>
    <dia:attribute name="guides">
      <dia:composite type="guides">
        <dia:attribute name="hguides"/>
        <dia:attribute name="vguides"/>
      </dia:composite>
    </dia:attribute>
  </dia:diagramdata>
  <dia:layer name="Background" visible="true">
		  <xsl:apply-templates select="//resultset[contains(@statement,'SHOW FULL COLUMNS FROM ')]"/>
  </dia:layer>
</dia:diagram>
</xsl:template>
<!-- ================ RESULT SET ========================================================== -->

<xsl:template match="resultset">
<xsl:variable name="tableName" select="normalize-space(substring(@statement,24))"/>

<dia:object type="Database - Table" version="0" id="{position()}">
	<xsl:variable name="width">5.5</xsl:variable>
	<xsl:variable name="height">3.0</xsl:variable>
	<xsl:variable name="x"><xsl:value-of select="number($width) * number( position() mod 5 )"/></xsl:variable>
	<xsl:variable name="y"><xsl:value-of select="number($height) * round( position() div 5 )"/></xsl:variable>
	


      <dia:attribute name="obj_pos">
	<xsl:element name="dia:point">
		<xsl:attribute name="val"><xsl:value-of select="$x"/>,<xsl:value-of select="$y"/></xsl:attribute>
	</xsl:element>        
      </dia:attribute>
      <dia:attribute name="obj_bb">
	<xsl:element name="dia:rectangle">
         <xsl:attribute name="val"><xsl:value-of select="$x"/>,<xsl:value-of select="$y"/>;<xsl:value-of select="$x + $width"/>,<xsl:value-of select="$y + $height"/></xsl:attribute>
        </xsl:element>
      </dia:attribute>
      <dia:attribute name="elem_corner">
       <xsl:element name="dia:point">
		<xsl:attribute name="val"><xsl:value-of select="$x"/>,<xsl:value-of select="$y"/></xsl:attribute>
	</xsl:element>   
      </dia:attribute>
      <dia:attribute name="elem_width">
	<xsl:element name="dia:real">
		<xsl:attribute name="val"><xsl:value-of select="$width"/></xsl:attribute>
	</xsl:element>  
      </dia:attribute>
      <dia:attribute name="elem_height">
	<xsl:element name="dia:real">
		<xsl:attribute name="val"><xsl:value-of select="$height"/></xsl:attribute>
	</xsl:element>  
      </dia:attribute>
      <dia:attribute name="name">
        <dia:string>#<xsl:value-of select="$tableName"/>#</dia:string>
      </dia:attribute>
      <dia:attribute name="comment">
        <dia:string>#Table <xsl:value-of select="$tableName"/>#</dia:string>
      </dia:attribute>
      <dia:attribute name="visible_comment">
        <dia:boolean val="true"/>
      </dia:attribute>
      <dia:attribute name="underline_primary_key">
        <dia:boolean val="false"/>
      </dia:attribute>
      <dia:attribute name="bold_primary_keys">
        <dia:boolean val="true"/>
      </dia:attribute>
      <dia:attribute name="normal_font">
        <dia:font family="monospace" style="0" name="Courier"/>
      </dia:attribute>
		<dia:attribute name="name_font"> 
				<dia:font family="sans" style="80" name="Helvetica-Bold"/> 
		</dia:attribute>
      <dia:attribute name="comment_font">
        <dia:font family="sans" style="8" name="Helvetica-Oblique"/>
      </dia:attribute>
      <dia:attribute name="normal_font_height">
        <dia:real val="0.80000000000000004"/>
      </dia:attribute>
	   <dia:attribute name="name_font_height"> 
		 <dia:real val="0.69999999999999996"/> 
		</dia:attribute>
      <dia:attribute name="comment_font_height">
        <dia:real val="0.69999999999999996"/>
      </dia:attribute>

      <dia:attribute name="attributes">

      <xsl:for-each select="row">

        <dia:composite type="table_attribute">
			<xsl:variable name="keyName"><xsl:value-of select="field[@name='Field']" /></xsl:variable>
          <dia:attribute name="name">
            <dia:string>#<xsl:value-of select="$keyName"/>#</dia:string>
          </dia:attribute>
		  <xsl:variable name="indexName">
				  <xsl:value-of select="/root/resultset[contains(@statement,'SHOW INDEX FROM') and contains(@statement, $tableName)]/row[count(field[@name='Column_name' and text()=$keyName ])>0]/field[@name='Key_name']/text()" />
		  </xsl:variable>
		  <xsl:variable name="not_uniq">
				  <xsl:value-of select="/root/resultset[contains(@statement,'SHOW INDEX FROM') and contains(@statement, $tableName)]/row[count(field[@name='Column_name' and text()=$keyName ])>0]/field[@name='Non_unique']/text()" />
		  </xsl:variable>
          <dia:attribute name="type">
				  <dia:string>#<xsl:value-of select="field[@name='Type']"/><xsl:if test="field[@name='Default']/text()">, default=<xsl:value-of select="field[@name='Default']" /></xsl:if><xsl:if test="string-length($indexName)>0">, <xsl:if test="$not_uniq=0">u_</xsl:if>index=<xsl:value-of select="$indexName" /></xsl:if><xsl:if test="contains(field[@name='Extra']/text(), 'auto_incre')">, AI</xsl:if>#</dia:string>
		  </dia:attribute>
	  <xsl:choose>
			  <xsl:when test="field[@name='Key']/text()='PRI'">
				<dia:attribute name="primary_key">
						<dia:boolean val='true' />
				</dia:attribute>
				<dia:attribute name="unique">
						<dia:boolean val='true' />
				</dia:attribute>
		</xsl:when>
		<xsl:when test="field[@name='Key']/text()='UNI'">
				<dia:attribute name="primary_key">
						<dia:boolean val='false' />
				</dia:attribute>
				<dia:attribute name="unique">
						<dia:boolean val='true' />
				</dia:attribute>
		</xsl:when>
		<xsl:otherwise>
				<dia:attribute name="primary_key">
						<dia:boolean val='false' />
				</dia:attribute>
				<dia:attribute name="unique">
						<dia:boolean val='false' />
				</dia:attribute>
		</xsl:otherwise>
      </xsl:choose>
		  <dia:attribute name="nullable">
				  <xsl:choose><xsl:when test="field[@name='Null']/text()='NO'">
						<dia:boolean val='false' />
				</xsl:when><xsl:otherwise>
						<dia:boolean val='true' />
				</xsl:otherwise>
				</xsl:choose>
		  </dia:attribute>
          <dia:attribute name="comment">
            <dia:string>#<xsl:value-of select="field[@name='Comment']"/>#</dia:string>
          </dia:attribute>
        </dia:composite>

      </xsl:for-each> 

      </dia:attribute>
    </dia:object>
   
</xsl:template>


</xsl:stylesheet>
